﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SweetSurvey.Models.ViewModels
{
    public class QuestionsViewModel: List<QuestionViewModel>
    {
        public QuestionsViewModel()
        {
        }

        public QuestionsViewModel(IEnumerable<Question> questions)
            : this()
        {
            foreach (var question in questions)
            {
                this.Add(new QuestionViewModel(question));
            }
        }
    }

    public class QuestionViewModel: IApplyChanges<Question>
    {
        public QuestionViewModel()
        {
            this.Options = new List<string>();
        }

        public QuestionViewModel(Question question)
            : this()
        {
            this.QuestionId = question.Id;
            this.Title = question.Title;
            var options = question.Options.OrderBy(o => o.Index);
            foreach (var option in options)
            {
                this.Options.Add(option.Text);
            }
            this.Index = question.Index;
        }

        public string QuestionId { get; set; }
        public string Title { get; set; }
        public List<string> Options { get; set; }
        public short Index { get; set; }

        #region IApplyChanges<Question> Members

        public void Apply(ref Question model)
        {
            model.Index = this.Index;
            model.Title = this.Title;
        }

        #endregion
    }
}